﻿using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;

using DTFight;

public class UIGameMgr : MonoBehaviour {

	static public UIGameMgr Instance = null;

	static public GameData CurRoom = null;
	static public PlayerData CurPlayer = null;
	static public GameData.GROUP_TYPE AdviserAnwser = GameData.GROUP_TYPE.MAX;

	static public GameData.GROUP_TYPE CurGroup { get; set; }

	public Sprite[] SptTipResults = new Sprite[3];
	public Image[] ImgTipResult = new Image[2];
	public Image[] ImgCards = new Image[(int)CardData.CARD_TYPE.MAX];
	public Text[] TxRightCard = new Text[(int)CardData.CARD_TYPE.MAX];
	public GameObject[] ObjStates = new GameObject[(int)GameData.STATE_FLAG.MAX];
	public GameObject ObjSelectGroup, ObjUnselectGroup, ObjBet, ObjWin, ObjLose, ObjNoBet;
	public Text TxPlayerPoints, TxBetPoints, TxWinPoint, TxLosePoint, TxNoBet, TxGameResult;
	public GameObject ObjAdviserAnwser;
	public Text TxAdviserAnwser;

	void Awake() {
		Instance = this;

		// init cards
		CardData.InitCardPool();
	}

	void OnDestroy()
	{
		Instance = null;
	}

	// Use this for initialization
	void Start () {
		CurGroup = GameData.GROUP_TYPE.MAX;
		Screen.orientation = ScreenOrientation.Landscape;
	}
	
	// Update is called once per frame
	void Update () {
		UpdateUI ();
	}

	void UpdateUI()
	{
		if (CurRoom == null)
			return;

		if (TxPlayerPoints != null)
			TxPlayerPoints.text = CurPlayer.Points.ToString ();

		if (TxBetPoints != null) {
			UInt32 curPoints = 0;
			for (int i=0; i<CurPlayer.BetPoints.Length; ++i)
			{
				curPoints += CurPlayer.BetPoints[i];
			}
			TxBetPoints.text = curPoints.ToString();
		}

		if (ObjBet != null) {
			if (CurGroup != GameData.GROUP_TYPE.MAX
				&& CurRoom.CurStateInfo.StateFlag != GameData.STATE_FLAG.OPEN_CARD_3
			    && CurRoom.CurStateInfo.StateFlag != GameData.STATE_FLAG.OPEN_CARD_4)
			{
				if (!ObjBet.activeInHierarchy)	ObjBet.SetActive(true);
			}
			else
			{
				if (ObjBet.activeInHierarchy)	ObjBet.SetActive(false);
			}
		}

		if (CurRoom.CurStateInfo.StateFlag == GameData.STATE_FLAG.SELECT_GROUP)
		{
			if (CurGroup == GameData.GROUP_TYPE.MAX)
			{
				if (ObjSelectGroup != null && ObjSelectGroup.activeInHierarchy)
					ObjSelectGroup.SetActive (false);

				if (ObjUnselectGroup != null && !ObjUnselectGroup.activeInHierarchy)	
					ObjUnselectGroup.SetActive (true);
			}
			else
			{
				if (ObjSelectGroup != null && !ObjSelectGroup.activeInHierarchy)
					ObjSelectGroup.SetActive (true);
				
				if (ObjUnselectGroup != null && ObjUnselectGroup.activeInHierarchy)	
					ObjUnselectGroup.SetActive (false);
			}
		}

		if (CurRoom.CurStateInfo.StateFlag == GameData.STATE_FLAG.SHOW_BET_RESULT)
		{
			if (CurRoom.CurStateInfo.StartTime.AddSeconds(CurRoom.CurStateInfo.WaiteTime) < DateTime.Now)
				CurRoom.CurStateInfo.StateFlag = GameData.STATE_FLAG.MAX;
		}

		for (int i=0; i<ObjStates.Length; ++i)
		{
			if (ObjStates[i] == null)
				continue;

			if ((GameData.STATE_FLAG)i == CurRoom.CurStateInfo.StateFlag)
			{
				if (!ObjStates[i].activeInHierarchy)
					ObjStates[i].SetActive(true);
			}
			else
			{
				if (ObjStates[i].activeInHierarchy)
					ObjStates[i].SetActive(false);
			}
		}

		for (int i=0; i<(int)CardData.CARD_TYPE.MAX; ++i) {
			if (CurRoom.DesktopCards[i] == null)
			{
				ShowCard((CardData.CARD_TYPE)i, 0);

				if (TxRightCard[i] != null && TxRightCard[i].enabled)
					TxRightCard[i].enabled = false;
			}
			else
			{
				ShowCard((CardData.CARD_TYPE)i, CurRoom.DesktopCards[i].CardId);

				if (TxRightCard[i] != null)
				{
					if ((int)CurRoom.DesktopCards[i].CardType == i)
					{
						if (!TxRightCard[i].enabled)	TxRightCard[i].enabled = true;
					}
				}
			}
		}

		if (AdviserAnwser != GameData.GROUP_TYPE.MAX)
		{
			if (ObjAdviserAnwser != null && !ObjAdviserAnwser.activeInHierarchy)
				ObjAdviserAnwser.SetActive(true);

			if (TxAdviserAnwser != null)	TxAdviserAnwser.text = AdviserAnwser.ToString();
		}
		else
		{
			if (ObjAdviserAnwser != null && ObjAdviserAnwser.activeInHierarchy)
				ObjAdviserAnwser.SetActive(false);
		}
	}

	public void Exit()
	{
		RoomMsg.Instance ().SubmitExitRoom ();
	}

	public void SelectD()
	{
		RoomMsg.Instance ().SubmitSelectGroup (GameData.GROUP_TYPE.龙潭);
	}

	public void SelectT()
	{
		RoomMsg.Instance ().SubmitSelectGroup (GameData.GROUP_TYPE.虎穴);
	}

	public void Bet(int point)
	{
		RoomMsg.Instance ().SubmitBetPoint ((uint)point);
	}

	public void GameStart()
	{
//		if (ObjGroup != null)
//			ObjGroup.SetActive (true);
	}

	public void SelectAdviser(string adviser)
	{
		GameData.ADVISER_TYPE type = (GameData.ADVISER_TYPE)Enum.Parse(typeof(GameData.ADVISER_TYPE), adviser);

		RoomMsg.Instance().SubmitSelectAdviser(type);

	}

	public void SelectGroup(GameData.GROUP_TYPE group)
	{
		CurRoom.SelectGroup (CurPlayer, group);

		CurGroup = group;

//		if (ObjGroup != null)
//			ObjGroup.SetActive (false);
	}

	public void ShowCard(CardData.CARD_TYPE slotType, byte cardId)
	{
		if (ImgCards [(int)slotType] == null)
			return;

		if (cardId == 0)
		{
			if (ImgCards [(int)slotType].sprite.name.Equals(cardId.ToString()))
				return;
			
			ImgCards [(int)slotType].sprite = Resources.Load<Sprite> ("Cards/"+cardId.ToString());
		}
		else
		{
			if (ImgCards [(int)slotType].sprite.name.Equals("b"+cardId.ToString()))
			    return;

			ImgCards [(int)slotType].sprite = Resources.Load<Sprite> ("Cards_Nine/b"+cardId.ToString());
		}
	}

	public void ShowResultTip()
	{
		if (CurRoom.CurStateInfo.StateFlag != GameData.STATE_FLAG.OPEN_CARD_1
			&& CurRoom.CurStateInfo.StateFlag != GameData.STATE_FLAG.OPEN_CARD_2
		    && CurRoom.CurStateInfo.StateFlag != GameData.STATE_FLAG.OPEN_CARD_3)
		{
			for (int i=0; i<ImgTipResult.Length; ++i)
			{
				if (ImgTipResult[i] == null)
					continue;

				ImgTipResult[i].enabled = false;
			}

			return;
		}

		int dragonNum = 0, tigerNum = 0;
		GameData.GROUP_TYPE winGroup = CurRoom.GetResult(ref dragonNum, ref tigerNum);

		for (int i=0; i<ImgTipResult.Length; ++i)
		{
			if (ImgTipResult[i] == null)
				continue;

			ImgTipResult[i].enabled = true;

			if (winGroup == GameData.GROUP_TYPE.MAX)
			{
				ImgTipResult[i].sprite = SptTipResults[0];
			}
			else
			{
				if (winGroup == (GameData.GROUP_TYPE)i)
					ImgTipResult[i].sprite = SptTipResults[1];
				else
					ImgTipResult[i].sprite = SptTipResults[2];
			}
		}
	}

	public void ShowGameResult(GameData.ResultInfo result)
	{
		if (TxGameResult != null)
			TxGameResult.text = result.WinGroup.ToString () + "赢!!!";

		if (CurGroup == GameData.GROUP_TYPE.MAX) {
			if (ObjNoBet != null)
				ObjNoBet.SetActive (true);
			if (ObjWin != null)
				ObjWin.SetActive (false);
			if (ObjLose != null)
				ObjLose.SetActive (false);

			if (TxNoBet != null)
				TxNoBet.text = CurPlayer.Points.ToString ();
		} else if (CurGroup != result.WinGroup) {
			ShowBetResult(false, CurPlayer.Points);
		}
	}

	public void ShowBetResult(bool bWin, UInt32 points)
	{
		if (ObjNoBet!= null)	ObjNoBet.SetActive(false);

		if (bWin) {
			if (ObjWin != null && !ObjWin.activeInHierarchy)	ObjWin.SetActive(true);
			if (ObjLose != null && ObjLose.activeInHierarchy)	ObjLose.SetActive(false);

			if (TxWinPoint != null)	TxWinPoint.text = points.ToString ();
		} else {
			if (ObjWin != null && ObjWin.activeInHierarchy)	ObjWin.SetActive(false);
			if (ObjLose != null && !ObjLose.activeInHierarchy)	ObjLose.SetActive(true);

			if (TxLosePoint != null) TxLosePoint.text = points.ToString ();
		}
	}
}
